********************************************************************************
******************************* Stack data *************************************
********************************************************************************
	
	use "ZA6804_v6-0-0_processed.dta", clear
	
	*** keep only left-authoritarians
	keep if w7_leftauth==1 
	
	
********************************************************************************
******************* Preparations for analysis of stacked data ******************
******************************************************************************** 
	*** Prepare controls for party scalometer analysis
	*** Create linear transformations of socio-demographic controls, i.e. obtain predicted values from simple regressions	
		
		* Age
		reg kp7_430a age
			predict cdu_age if e(sample), xb
		reg kp7_430b age
			predict csu_age if e(sample), xb
		reg kp7_430c age
			predict spd_age if e(sample), xb
		reg kp7_430d age
			predict fdp_age if e(sample), xb
		reg kp7_430e age
			predict gruen_age if e(sample), xb
		reg kp7_430f age
			predict linke_age if e(sample), xb
		reg kp7_430i age
			predict afd_age if e(sample), xb
		
		* female
		reg kp7_430a female
			predict cdu_female if e(sample), xb
		reg kp7_430b female
			predict csu_female if e(sample), xb
		reg kp7_430c female
			predict spd_female if e(sample), xb
		reg kp7_430d female
			predict fdp_female if e(sample), xb
		reg kp7_430e female
			predict gruen_female if e(sample), xb
		reg kp7_430f female
			predict linke_female if e(sample), xb
		reg kp7_430i female
			predict afd_female if e(sample), xb
			
		* east
		reg kp7_430a east
			predict cdu_east if e(sample), xb
		reg kp7_430b east
			predict csu_east if e(sample), xb
		reg kp7_430c east
			predict spd_east if e(sample), xb
		reg kp7_430d east
			predict fdp_east if e(sample), xb
		reg kp7_430e east
			predict gruen_east if e(sample), xb
		reg kp7_430f east
			predict linke_east if e(sample), xb
		reg kp7_430i east
			predict afd_east if e(sample), xb
			
		* christian
		reg kp7_430a christian_d
			predict cdu_christian_d if e(sample), xb
		reg kp7_430b christian_d
			predict csu_christian_d if e(sample), xb
		reg kp7_430c christian_d
			predict spd_christian_d if e(sample), xb
		reg kp7_430d christian_d
			predict fdp_christian_d if e(sample), xb
		reg kp7_430e christian_d
			predict gruen_christian_d if e(sample), xb
		reg kp7_430f christian_d
			predict linke_christian_d if e(sample), xb
		reg kp7_430i christian_d
			predict afd_christian_d if e(sample), xb
			
		* rural
		reg kp7_430a rural2
			predict cdu_rural2 if e(sample), xb
		reg kp7_430b rural2
			predict csu_rural2 if e(sample), xb
		reg kp7_430c rural2
			predict spd_rural2 if e(sample), xb
		reg kp7_430d rural2
			predict fdp_rural2 if e(sample), xb
		reg kp7_430e rural2
			predict gruen_rural2 if e(sample), xb
		reg kp7_430f rural2
			predict linke_rural2 if e(sample), xb
		reg kp7_430i rural2
			predict afd_rural2 if e(sample), xb
			
		* education
		reg kp7_430a i.educat
			predict cdu_educat if e(sample), xb
		reg kp7_430b i.educat
			predict csu_educat if e(sample), xb
		reg kp7_430c i.educat
			predict spd_educat if e(sample), xb
		reg kp7_430d i.educat
			predict fdp_educat if e(sample), xb
		reg kp7_430e i.educat
			predict gruen_educat if e(sample), xb
		reg kp7_430f i.educat
			predict linke_educat if e(sample), xb
		reg kp7_430i i.educat
			predict afd_educat if e(sample), xb
			
		* income
		reg kp7_430a income_st
			predict cdu_income_st if e(sample), xb
		reg kp7_430b income_st
			predict csu_income_st if e(sample), xb
		reg kp7_430c income_st
			predict spd_income_st if e(sample), xb
		reg kp7_430d income_st
			predict fdp_income_st if e(sample), xb
		reg kp7_430e income_st
			predict gruen_income_st if e(sample), xb
		reg kp7_430f income_st
			predict linke_income_st if e(sample), xb
		reg kp7_430i income_st
			predict afd_income_st if e(sample), xb
		
		* left-right
		reg kp7_430a i.w7_leftright_cat
			predict cdu_lrcat if e(sample), xb
		reg kp7_430b i.w7_leftright_cat
			predict csu_lrcat if e(sample), xb
		reg kp7_430c i.w7_leftright_cat
			predict spd_lrcat if e(sample), xb
		reg kp7_430d i.w7_leftright_cat
			predict fdp_lrcat if e(sample), xb
		reg kp7_430e i.w7_leftright_cat 
			predict gruen_lrcat if e(sample), xb
		reg kp7_430f i.w7_leftright_cat 
			predict linke_lrcat if e(sample), xb
		reg kp7_430i i.w7_leftright_cat 
			predict afd_lrcat if e(sample), xb


	
	* stack data with seperate entries for CDU and CSU
	
	gen personid = _n
	expand 7
	bysort personid: gen obs_party = _n
	label define obs_party 1 "CDU" 2 "CSU" 3 "SPD" 4 "FDP" 5 "Greens" 6 "The Left" 7 "AfD"
	label values obs_party obs_party
	
		
********************************************************************************
************************ Recode stacked data ***********************************
********************************************************************************	
	
	* recodings for stacked data with seperate entries for CDU and CSU

	*** skalometer
		gen skalometer = kp7_430a if obs_party == 1
		replace skalometer = kp7_430b if obs_party == 2
		replace skalometer = kp7_430c if obs_party == 3
		replace skalometer = kp7_430d if obs_party == 4
		replace skalometer = kp7_430e if obs_party == 5
		replace skalometer = kp7_430f if obs_party == 6
		replace skalometer = kp7_430i if obs_party == 7
	
	*** perceived party positions
		* perceived party position economy
		gen w7_party_highertaxes = w7_highertaxes_cdu if obs_party == 1
		replace w7_party_highertaxes = w7_highertaxes_csu if obs_party == 2
		replace w7_party_highertaxes = w7_highertaxes_spd if obs_party == 3
		replace w7_party_highertaxes = w7_highertaxes_fdp if obs_party == 4
		replace w7_party_highertaxes = w7_highertaxes_gruene if obs_party == 5
		replace w7_party_highertaxes = w7_highertaxes_linke if obs_party == 6
		replace w7_party_highertaxes = w7_highertaxes_afd if obs_party == 7
		
		gen w7_party_highertaxes_st=(w7_party_highertaxes-1)/6

		* perceived party position migration
		gen w7_party_migrrestr = w7_migrrestr_cdu if obs_party == 1
		replace w7_party_migrrestr = w7_migrrestr_csu if obs_party == 2
		replace w7_party_migrrestr = w7_migrrestr_spd if obs_party == 3
		replace w7_party_migrrestr = w7_migrrestr_fdp if obs_party == 4
		replace w7_party_migrrestr = w7_migrrestr_gruene if obs_party == 5
		replace w7_party_migrrestr = w7_migrrestr_linke if obs_party == 6
		replace w7_party_migrrestr = w7_migrrestr_afd if obs_party == 7
		
		gen w7_party_migrrestr_st=(w7_party_migrrestr-1)/6

		* dichotomize perceived party position
		recode w7_party_highertaxes (1 2 3 4=0) (5 6 7=1), gen(w7_party_highertaxes_dum)
		recode w7_party_migrrestr (1 2 3 4=0) (5 6 7=1), gen(w7_party_migrrestr_dum)
		
		* perception of left-authoritarian position
		gen w7_party_leftauth=0 if w7_party_highertaxes<. & w7_party_migrrestr<.
		replace w7_party_leftauth=1 if w7_party_highertaxes_dum==1 & w7_party_migrrestr_dum==1
		
			* and pecreption of location in other quadrants
			gen w7_party_rightauth=0 if w7_party_highertaxes<. & w7_party_migrrestr<.
			replace w7_party_rightauth=1 if w7_party_highertaxes>=1 & w7_party_highertaxes<=3 & w7_party_migrrestr>=5 & w7_party_migrrestr<=7
			
			gen w7_party_rightlib=0 if w7_party_highertaxes<. & w7_party_migrrestr<.
			replace w7_party_rightlib=1 if w7_party_highertaxes>=1 & w7_party_highertaxes<=3 & w7_party_migrrestr>=1 & w7_party_migrrestr<=3
			
			gen w7_party_leftlib=0 if w7_party_highertaxes<. & w7_party_migrrestr<.
			replace w7_party_leftlib=1 if w7_party_highertaxes>=5 & w7_party_highertaxes<=7 & w7_party_migrrestr>=1 & w7_party_migrrestr<=3

		* perceived party position on leftright
		gen w7_party_lr = kp7_1490a if obs_party == 1
		replace w7_party_lr = kp7_1490b if obs_party == 2
		replace w7_party_lr = kp7_1490c if obs_party == 3
		replace w7_party_lr = kp7_1490d if obs_party == 4
		replace w7_party_lr = kp7_1490e if obs_party == 5
		replace w7_party_lr = kp7_1490f if obs_party == 6
		replace w7_party_lr = kp7_1490i if obs_party == 7
		
		gen w7_party_lr_st=(w7_party_lr-1)/10
		

	*** perceived distance
		gen dist_subj_migration=abs(w7_migrrestr_ego_st-w7_party_migrrestr_st)
		gen dist_subj_econ=abs(w7_highertaxes_ego_st-w7_party_highertaxes_st)
		gen dist_subj_leftright=abs(w7_leftright_st-w7_party_lr_st)
	
	
	*** objective party positions
		*objective party position economy: mean lrecon 2017 CHES
		gen CHES_party_lrecon=6.0714287758 if obs_party == 1
		replace CHES_party_lrecon=6.1428570747 if obs_party == 2
		replace CHES_party_lrecon=3.3571429253 if obs_party == 3
		replace CHES_party_lrecon=8.2142858505 if obs_party == 4
		replace CHES_party_lrecon=3.0714285374 if obs_party == 5
		replace CHES_party_lrecon=1 if obs_party == 6
		replace CHES_party_lrecon=7.6153845787 if obs_party == 7
		
		gen CHES_party_lrecon_st=(-CHES_party_lrecon+10)/10
		
		*objective party position migration: mean immigrate_policy 2017 CHES
		gen CHES_party_migrrestr=5.8571429252624512 if obs_party == 1
		replace CHES_party_migrrestr=7.7857141494750977 if obs_party == 2
		replace CHES_party_migrrestr=3.8571429252624512 if obs_party == 3
		replace CHES_party_migrrestr=6.4285712242126465 if obs_party == 4
		replace CHES_party_migrrestr=2.3571429252624512 if obs_party == 5
		replace CHES_party_migrrestr=3.3571429252624512 if obs_party == 6
		replace CHES_party_migrrestr=9.2142858505249023 if obs_party == 7
		
		gen CHES_party_migrrestr_st=(CHES_party_migrrestr)/10

		*objective party position leftright: mean leftright_policy 2017 CHES
		gen CHES_party_leftright=5.6428570747 if obs_party == 1
		replace CHES_party_leftright=7.5 if obs_party == 2
		replace CHES_party_leftright=3.9285714626 if obs_party == 3
		replace CHES_party_leftright=6.5714287758 if obs_party == 4
		replace CHES_party_leftright=2.9285714626 if obs_party == 5
		replace CHES_party_leftright=1.4285714626 if obs_party == 6
		replace CHES_party_leftright=9.2142858505 if obs_party == 7
		
		gen CHES_party_leftright_st=CHES_party_leftright/10
	
		* objective distance variables 
		gen dist_obj_migration=abs(w7_migrrestr_ego_st-CHES_party_migrrestr_st)
		gen dist_obj_econ=abs(w7_highertaxes_ego_st-CHES_party_lrecon_st)
		gen dist_obj_leftright=abs(w7_leftright_st-CHES_party_leftright_st)
		
	
	*** party identification dummy
		gen partyid_dummy=0 if kp7_2090a !=.a & kp7_2090a != .e & kp7_2090a != .g
		replace partyid_dummy=1 if obs_party == 1 & (kp7_2090a==1 | kp7_2090a==2)
		replace partyid_dummy=1 if obs_party == 2 & (kp7_2090a==1 | kp7_2090a==3)
		replace partyid_dummy=1 if obs_party == 3 & kp7_2090a==4
		replace partyid_dummy=1 if obs_party == 4 & kp7_2090a==5
		replace partyid_dummy=1 if obs_party == 5 & kp7_2090a==6
		replace partyid_dummy=1 if obs_party == 6 & kp7_2090a==7
		replace partyid_dummy=1 if obs_party == 7 & kp7_2090a==322

	
	** socio-demogarphic controls		
		* age
		gen age_party = cdu_age if obs_party == 1
		replace age_party = csu_age if obs_party == 2
		replace age_party = spd_age if obs_party == 3
		replace age_party = fdp_age if obs_party == 4
		replace age_party = gruen_age if obs_party == 5
		replace age_party = linke_age if obs_party == 6
		replace age_party = afd_age if obs_party == 7
		
		* female
		gen female_party = cdu_female if obs_party == 1
		replace female_party = csu_female if obs_party == 2
		replace female_party = spd_female if obs_party == 3
		replace female_party = fdp_female if obs_party == 4
		replace female_party = gruen_female if obs_party == 5
		replace female_party = linke_female if obs_party == 6
		replace female_party = afd_female if obs_party == 7
		
		* east
		gen east_party = cdu_east if obs_party == 1
		replace east_party = csu_east if obs_party == 2
		replace east_party = spd_east if obs_party == 3
		replace east_party = fdp_east if obs_party == 4
		replace east_party = gruen_east if obs_party == 5
		replace east_party = linke_east if obs_party == 6
		replace east_party = afd_east if obs_party == 7
		
		* christian
		gen christian_party = cdu_christian_d if obs_party == 1
		replace christian_party = csu_christian_d if obs_party == 2
		replace christian_party = spd_christian_d if obs_party == 3
		replace christian_party = fdp_christian_d if obs_party == 4
		replace christian_party = gruen_christian_d if obs_party == 5
		replace christian_party = linke_christian_d if obs_party == 6
		replace christian_party = afd_christian_d if obs_party == 7
				
		* rural
		gen rural_party = cdu_rural2 if obs_party == 1
		replace rural_party = csu_rural2 if obs_party == 2
		replace rural_party = spd_rural2 if obs_party == 3
		replace rural_party = fdp_rural2 if obs_party == 4
		replace rural_party = gruen_rural2 if obs_party == 5
		replace rural_party = linke_rural2 if obs_party == 6
		replace rural_party = afd_rural2 if obs_party == 7
		
		* education
		gen educat_party = cdu_educat if obs_party == 1
		replace educat_party = csu_educat if obs_party == 2
		replace educat_party = spd_educat if obs_party == 3
		replace educat_party = fdp_educat if obs_party == 4
		replace educat_party = gruen_educat if obs_party == 5
		replace educat_party = linke_educat if obs_party == 6
		replace educat_party = afd_educat if obs_party == 7
		
		* left right
		gen lrcat_party = cdu_lrcat if obs_party == 1
		replace lrcat_party = csu_lrcat if obs_party == 2
		replace lrcat_party = spd_lrcat if obs_party == 3
		replace lrcat_party = fdp_lrcat if obs_party == 4
		replace lrcat_party = gruen_lrcat if obs_party == 5
		replace lrcat_party = linke_lrcat if obs_party == 6
		replace lrcat_party = afd_lrcat if obs_party == 7
		
		* income
		gen income_party = cdu_income_st if obs_party == 1
		replace income_party = csu_income_st if obs_party == 2
		replace income_party = spd_income_st if obs_party == 3
		replace income_party = fdp_income_st if obs_party == 4
		replace income_party = gruen_income_st if obs_party == 5
		replace income_party = linke_income_st if obs_party == 6
		replace income_party = afd_income_st if obs_party == 7

		
	*** label some variables
		label variable w7_party_highertaxes_dum "Party perceived as economically left"
		label variable w7_party_migrrestr_dum "Party perceived as restrictive on migration"
		label variable w7_party_leftauth "Party perceived as left-authoritarian"
		label variable w7_party_rightauth "Party perceived as right-authoritarian"
		label variable w7_party_leftlib "Party perceived as left-libertarian"
		label variable w7_party_rightlib "Party perceived as right-libertarian"
		label variable w7_mip_econ "MIP economic"
		label variable w7_mip_immi "MIP immigration"
		label variable w7_party_highertaxes_st "Perceived economic party position"
		label variable w7_party_migrrestr_st "Perceived party position on immigration"
		label variable dist_subj_econ "Perceived economic distance"
		label variable dist_subj_migration "Perceived distance on immigration"
		label variable dist_subj_leftright "Perceived distance on left-right"
		label variable lrcat_party "left-right (categorical, transformed)"
		label variable w7_party_lr "perceived left-right position of party"